package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.*;
import com.bjpowernode.crm.workbench.mapper.*;
import com.bjpowernode.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("clueService")
public class ClueServiceImpl implements ClueService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ContactsMapper contactsMapper;

    @Autowired
    private ClueRemarkMapper clueRemarkMapper;

    @Autowired
    private CustomerRemarkMapper customerRemarkMapper;

    @Autowired
    private ContactsRemarkMapper contactsRemarkMapper;

    @Autowired
    private ClueActivityRelationMapper clueActivityRelationMapper;

    @Autowired
    private ContactsActivityRelationMapper contactsActivityRelationMapper;

    @Autowired
    private TranMapper tranMapper;

    @Autowired
    private TranRemarkMapper tranRemarkMapper;

    //保存创建的线索
    @Override
    public int saveCreateClue(Clue clue) {
        return clueMapper.insertClue(clue);
    }

    //通过条件分页查询数据
    @Override
    public List<Clue> queryClueByConditionForPage(Map<String, Object> map) {
        return clueMapper.selectClueByConditionForPage(map);
    }

    //根据条件查询线索的总条数
    @Override
    public int queryCountOfClueByCondition(Map<String, Object> map) {
        return clueMapper.selectCountOfClueByCondition(map);
    }

    //根据ids批量删除线索
    @Override
    public int deleteClueByIds(String[] ids) {
        return clueMapper.deleteClueByIds(ids);
    }

    //根据id查询线索的信息
    @Override
    public Clue queryClueById(String id) {
        return clueMapper.selectClueById(id);
    }

    //保存修改的市场活动
    @Override
    public int saveEditClue(Clue clue) {
        return clueMapper.updateClue(clue);
    }

    //根据线索的id查看详细信息
    @Override
    public Clue queryClueForDetailById(String id) {
        return clueMapper.selectClueForDetailById(id);
    }

    //线索转换-----该方法应用了事务
    @Override
    public void saveConvertClue(Map<String, Object> map) {
        String clueId=(String) map.get("clueId");
        User user=(User) map.get(Contants.SESSION_USER);
        String isCreateTran=(String) map.get("isCreateTran");
        //根据id查询线索的信息
        Clue clue=clueMapper.selectClueById(clueId);
        //把该线索中有关公司的信息转换到客户表中
        Customer c=new Customer();//创建客户
        c.setAddress(clue.getAddress());//地址
        c.setContactSummary(clue.getContactSummary());//联系纪要
        c.setCreateBy(user.getId());//创造者id
        c.setCreateTime(DateUtils.formateDateTime(new Date()));//创造时间
        c.setDescription(clue.getDescription());//描述
        c.setId(UUIDUtils.getUUID());//新添加的数据的id
        c.setName(clue.getCompany());//公司的名字
        c.setNextContactTime(clue.getNextContactTime());//下次联系时间
        c.setOwner(user.getId());//所有者，谁创建的谁负责
        c.setPhone(clue.getPhone());//公司座机
        c.setWebsite(clue.getWebsite());//公司网站
        customerMapper.insertCustomer(c);//------------------保存创建的客户
        //把该线索中有关个人的信息转换到联系人表中
        Contacts co=new Contacts();
        co.setAddress(clue.getAddress());//地址
        co.setAppellation(clue.getAppellation());//称呼
        co.setContactSummary(clue.getContactSummary());//联系纪要
        co.setCreateBy(user.getId());//创造者id
        co.setCreateTime(DateUtils.formateDateTime(new Date()));//创造时间
        co.setCustomerId(c.getId());//所在公司的id
        co.setDescription(clue.getDescription());//描述
        co.setEmail(clue.getEmail());//邮箱
        co.setFullname(clue.getFullname());//联系人名字
        co.setId(UUIDUtils.getUUID());//新添加的数据的id
        co.setJob(clue.getJob());//联系人的工作
        co.setMphone(clue.getMphone());//联系人的电话
        co.setNextContactTime(clue.getNextContactTime());//下次联系时间
        co.setOwner(user.getId());//所有者，谁创建的谁负责
        co.setSource(clue.getSource());//来源
        contactsMapper.insertContacts(co);//------------------保存创建的联系人

        //根据clueId查询该线索下所有的备注
        List<ClueRemark> crList=clueRemarkMapper.selectClueRemarkByClueId(clueId);

        //如果该线索下有备注，把该线索下所有的备注转换到客户备注表中一份,把该线索下所有的备注转换到联系人备注表中一份
        if(crList!=null&&crList.size()>0){
            //遍历crList，封装客户备注
            CustomerRemark cur=null;
            ContactsRemark cor=null;
            List<CustomerRemark> curList=new ArrayList<>();
            List<ContactsRemark> corList=new ArrayList<>();
            for(ClueRemark cr:crList){
                cur=new CustomerRemark();//客户备注
                cur.setCreateBy(cr.getCreateBy());
                cur.setCreateTime(cr.getCreateTime());
                cur.setCustomerId(c.getId());
                cur.setEditBy(cr.getEditBy());
                cur.setEditFlag(cr.getEditFlag());
                cur.setEditTime(cr.getEditTime());
                cur.setId(UUIDUtils.getUUID());
                cur.setNoteContent(cr.getNoteContent());
                curList.add(cur);

                cor=new ContactsRemark();
                cor.setContactsId(co.getId());
                cor.setCreateBy(cr.getCreateBy());
                cor.setCreateTime(cr.getCreateTime());
                cor.setEditBy(cr.getEditBy());
                cor.setEditFlag(cr.getEditFlag());
                cor.setEditTime(cr.getEditTime());
                cor.setId(UUIDUtils.getUUID());
                cor.setNoteContent(cr.getNoteContent());
                corList.add(cor);
            }
            customerRemarkMapper.insertCustomerRemarkByList(curList);//---------批量保存创建的客户备注
            contactsRemarkMapper.insertContactsRemarkByList(corList);//---------批量保存创建的联系人备注

        }

        //根据clueId查询该线索和市场活动的关联关系
        List<ClueActivityRelation> carList=clueActivityRelationMapper.selectClueActivityRelationByClueId(clueId);

        //把该线索和市场活动的关联关系转换到联系人和市场活动的关联关系表中
        if(carList!=null&&carList.size()>0){
            ContactsActivityRelation coar=null;
            List<ContactsActivityRelation> coarList=new ArrayList<>();
            for(ClueActivityRelation car:carList){
                coar=new ContactsActivityRelation();
                coar.setActivityId(car.getActivityId());
                coar.setContactsId(co.getId());
                coar.setId(UUIDUtils.getUUID());
                coarList.add(coar);
            }
            contactsActivityRelationMapper.insertContactsActivityRelationByList(coarList);//批量保存创建的联系人和市场活动的关联关系
        }

        //如果需要创建交易，则往交易表中添加一条记录,还需要把该线索下的备注转换到交易备注表中一份
        if("true".equals(isCreateTran)){
            Tran tran=new Tran();
            tran.setActivityId((String) map.get("activityId"));//市场活动源
            tran.setContactsId(co.getId());//该交易联系人的id
            tran.setCreateBy(user.getId());//创造者的id
            tran.setCreateTime(DateUtils.formateDateTime(new Date()));//创造时间
            tran.setCustomerId(c.getId());//该交易公司的id
            tran.setExpectedDate((String) map.get("expectedDate"));//预计成交日期
            tran.setId(UUIDUtils.getUUID());//该交易的id
            tran.setMoney((String) map.get("money"));//该交易的金额
            tran.setName((String) map.get("name"));//该交易的名字
            tran.setOwner(user.getId());
            tran.setStage((String) map.get("stage"));//该交易的阶段
            tranMapper.insertTran(tran);//--------------------------保存创建的交易

            if(crList!=null&&crList.size()>0){
                TranRemark tr=null;
                List<TranRemark> trList=new ArrayList<>();
                for(ClueRemark cr:crList){
                    tr=new TranRemark();//交易备注
                    tr.setCreateBy(cr.getCreateBy());
                    tr.setCreateTime(cr.getCreateTime());
                    tr.setEditBy(cr.getEditBy());
                    tr.setEditFlag(cr.getEditFlag());
                    tr.setEditTime(cr.getEditTime());
                    tr.setId(UUIDUtils.getUUID());
                    tr.setNoteContent(cr.getNoteContent());
                    tr.setTranId(tran.getId());
                    trList.add(tr);
                }

                tranRemarkMapper.insertTranRemarkByList(trList);
            }
        }

        //删除该线索下所有的备注-----根据clueId删除该线索下所有的备注
        clueRemarkMapper.deleteClueRemarkByClueId(clueId);

        //删除该线索和市场活动的关联关系-----根据clueId删除线索和市场活动的关联关系
        clueActivityRelationMapper.deleteClueActivityRelationByClueId(clueId);

        //删除线索-----根据id删除线索
        clueMapper.deleteClueById(clueId);

    }
}
